// generator 函数
// 惰性执行

function* foo() {
    console.log('zce');
    return 100;
}

const result = foo();
console.log(result.next());

function* bar() {
    console.log('111');
    yield 100;
    console.log('222');
    yield 200;
    console.log('333');
    yield 300;
}

const generator = bar();
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());

// 应用

// 1. 发号器

function* createIdMaker() {
    let id = 1;
    while (true) {
        yield id++;
    }
}

const idMaker = createIdMaker();

console.log(idMaker.next());
console.log(idMaker.next());

// 2. 迭代器
const todos = {
    life: ['吃饭', '睡觉', '打豆豆'],
    learn: ['语文', '数学', '外语'],
    work: ['喝茶'],
    [Symbol.iterator]: function* () {
        const all = [].concat(this.life, this.learn, this.work);
        for (const item of all) {
            yield item;
        }
    },
};

for (const item of todos) {
    console.log(item);
}
